package basic.datastruct.二叉树;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/12 19:45
 * @描述 暂无
 */
public class BinaryTreee {
    public static void main(String[] args) {
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(2);
        TreeNode n3 = new TreeNode(3);
        TreeNode n4 = new TreeNode(4);
        TreeNode n5 = new TreeNode(5);

        n1.setLeft(n2);
        n1.setRight(n3);
        n3.setLeft(n4);
        n3.setRight(n5);
        n1.printQx();
        System.out.println();
        n1.printZx();
        System.out.println();
        n1.printHx();
        System.out.println("\n");
        BinaryTree tree = new BinaryTree();
        tree.setRoot(n1);
        System.out.println("prePrint\n");
        tree.prePrint();
        System.out.println("infixPrint\n");
        tree.infixPrint();
        System.out.println("afterPrint\n");
        tree.afterPrint();

        System.out.println("target1:   " + n1.preFindTarget(3));
        System.out.println("target1:   " + n1.preFindTarget(2));
    }

    static class BinaryTree {

        private TreeNode root;

        public void setRoot(TreeNode root) {
            this.root = root;
        }

        public void prePrint() {
            if (this.root != null) {
                this.root.printQx();
            } else {
                System.out.println("树为空！");
            }

        }

        public void infixPrint() {
            if (this.root != null) {
                this.root.printZx();
            } else {
                System.out.println("树为空！");
            }

        }

        public void afterPrint() {
            if (this.root != null) {
                this.root.printHx();
            } else {
                System.out.println("树为空！");
            }

        }

        public TreeNode searchTargetPre(int value) {
            if (this.root != null) {
                return this.root.preFindTarget(value);
            } else {
                return null;
            }
        }

        public TreeNode searchTargetInfix(int value) {
            if (this.root != null) {
                return this.root.infixFindTarget(value);
            } else {
                return null;
            }
        }
//        public TreeNode searchTargetAfter(int value){
//            if(this.root!=null){
//                return this.root.
//            }
//        }
    }

    static class TreeNode {
        private int value;
        private TreeNode left;
        private TreeNode right;

        TreeNode(int value) {
            this.setValue(value);
        }


        public TreeNode preFindTarget(int value) {
            if (this.value == value) {
                return this;
            }
            TreeNode ans = null;
            if (this.left != null) {
                ans = this.left.preFindTarget(value);
            }
            //不加的话会去返回右边的空值
            if (ans != null) {
                return ans;
            }
            if (this.right != null) {
                ans = this.right.preFindTarget(value);
            }
            return ans;
        }

        public TreeNode infixFindTarget(int value) {
            TreeNode ans = null;
            if (this.left != null) {
                ans = this.left.infixFindTarget(value);
            }
            if (ans != null) {
                return ans;
            }
            if (this.value == value) {
                return this;
            }
            if (this.right != null) {
                ans = this.right.infixFindTarget(value);
            }
            return ans;
        }

        public void printQx() {
            System.out.println(this);
            if (this.left != null) {
                this.left.printQx();
            }
            if (this.right != null) {
                this.right.printQx();
            }
        }

        public void printZx() {
            if (this.left != null) {
                this.left.printZx();
            }
            System.out.println(this);
            if (this.right != null) {
                this.right.printZx();
            }
        }

        public void printHx() {
            if (this.left != null) {
                this.left.printHx();
            }
            if (this.right != null) {
                this.right.printHx();
            }
            System.out.println(this);
        }

        @Override
        public String toString() {

            return new String("NodeValue: " + this.value);
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }


    }

}
